package com.example.bookmanagementsystem.controller;

import com.example.bookmanagementsystem.constant.Constants;
import com.example.bookmanagementsystem.entity.*;
import com.example.bookmanagementsystem.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Controller
@ResponseBody
@Slf4j
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping(path = "/getBookListByPage")
    public Result<PageResult<BookInfo>> getBookInfoByPage(PageRequest pageRequest, HttpSession httpSession) {
        log.info("查询翻页信息, pageRequest:{}", pageRequest);
        ////用户登录校验
        //UserInfo userInfo = (UserInfo) httpSession.getAttribute(Constants.SESSION_USER_KEY);
        //if (userInfo == null || userInfo.getId() <= 0 || "".equals(userInfo.getUserName())) {
        //    //用户未登录
        //    return Result.unLogin();
        //}

        if (pageRequest.getPageSize() < 1 || pageRequest.getCurrentPage() < 1) {
            return Result.fail("参数校验失败");
        }
        PageResult<BookInfo> bookInfoPageResult = null;
        try {
            bookInfoPageResult = bookService.getBookInfoByPage(pageRequest);
        } catch (Exception e) {
            log.error("查询翻页信息错误! e:{}", e);
            return Result.fail(e.getMessage());
        }

        return Result.success(bookInfoPageResult);
    }

    //这里的produces = "application/json", 告诉客户端服务器会返回 JSON 数据，客户端可以根据这个信息处理响应
    //因为通过统一数据格式的返回,如果Controller的返回值是String的话,那么返回是Jackson序列化好的字符串,不符合json格式
    //如果是自定义类型,那么会自动转换成json,也就是响应的头部加上了application/json
    @RequestMapping(path = "/addBook", produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        log.info("接收到添加图书请求, bookInfo:{}", bookInfo);
        if (!StringUtils.hasLength(bookInfo.getBookName()) ||
                !StringUtils.hasLength(bookInfo.getAuthor()) ||
                bookInfo.getCount() < 0 ||
                bookInfo.getPrice() == null ||
                bookInfo.getPrice().compareTo(BigDecimal.ZERO) < 0 ||
                !StringUtils.hasLength(bookInfo.getPublish())) {
            return "参数校验失败, 请检查输入参数";
        }

        Integer result = bookService.addBook(bookInfo);
        if (result <= 0) {
            log.error("添加图书出错:bookInfo:{}", bookInfo);
            return "添加图书出错，请联系管理人";
        }
        return "";
    }

    @RequestMapping(path = "/queryBookInfoById")
    public BookInfo queryBookInfoById(Integer bookId) {
        log.info("根据ID查询图书, bookId:" + bookId);
        BookInfo bookInfo = null;
        try {
            bookInfo = bookService.getBookInfoById(bookId);
        } catch (Exception e) {
            log.error("查询图书失败, e:{}", e);
        }
        return bookInfo;
    }

    @RequestMapping(path = "/updateBook", produces = "application/json")
    public String updateBook(BookInfo bookInfo) {
        log.info("接收到更新图书请求, bookInfo:{}", bookInfo);

        Integer result = bookService.updateBook(bookInfo);
        if (result <= 0) {
            log.error("更新图书失败, 请联系管理员");
            return "更新图书失败, 请联系管理员";
        }
        return "";
    }

    @RequestMapping(path = "/batchDelete", produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids) {
        log.info("接收请求, 批量删除图书, 图书ID:{}",ids);
        Integer result = bookService.batchDelete(ids);
        if (result <= 0) {
            log.error("更新图书失败, 请联系管理员");
            return "更新图书失败, 请联系管理员";
        }
        return "";
    }
}
